<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    on-ok="handleOk"
    switch-fullscreen
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading" style="margin-bottom: 20px">
      <ants-form-container :disabled="formDisabled">
        <a-form slot="detail" :form="form">
          <a-row>
            <a-col :span="24">
              <a-form-item label="移动到" :label-col="labelCol" :wrapper-col="wrapperCol">
                <a-select
                  v-decorator="['selectMainId', validatorRules.selectMainId]"
                  style="width: 100%"
                  placeholder="请选择移动的文件夹"
                >
                  <a-select-option v-for="item in collectionList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </ants-form-container>
    </a-spin>

  </a-modal>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
import AntsFormContainer from '@/components/ants/AntsFormContainer'

export default {
  name: 'MoveFavoritesModal',
  components: {
    AntsFormContainer
  },
  data() {
    return {
      title: '移动至其他收藏夹',
      width: 600,
      visible: false,

      collectionList: [],
      url: {
        initList: '/articleFavorites/getByUser',
        moveFavorites: '/articleFavoritesSub/moveFavorites'
      },
      moveSelectKeys: [],
      mainId: '',

      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        selectMainId: {
          rules: [
            { required: true, message: '请选择移动的文件夹!' }
          ]
        }
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    }
  },
  methods: {
    click(moveSelectKeys) {
      this.moveSelectKeys = moveSelectKeys
      // 加在当前登陆人收藏夹
      getAction(this.url.initList, {}).then(res => {
        if (res.success) {
          this.collectionList = res.result
        }
      })
      this.visible = true
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {

      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {

          const formData = Object.assign(this.model, values)
          if (formData.selectMainId === this.mainId) {
            this.$message.info("文件已经在此文件夹中了！")
          } else {
            this.confirmLoading = true
            formData.moveSelectKeys = this.moveSelectKeys
            formData.oldMainId = this.mainId

            postAction(this.url.moveFavorites, formData).then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.handleCancel()
              } else {
                this.$message.warning(res.message)
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          }

        }
      })
    }
  }
}
</script>

<style scoped>

</style>
